<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 订单查找 -->
      <el-row>
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="queryParams.query" clearable @clear="getOrders">
            <el-button slot="append" icon="el-icon-search" @click="getOrders"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表 -->
      <el-table :data="orders" border stripe>
        <el-table-column type="index"/>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单详情" prop="order_price"></el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '1'" type="success">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showSetAddrDialog"/>
            <!-- 物流进度按钮 -->
            <el-button type="success" size="mini" icon="el-icon-location" @click="showProgressDialog"/>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[1, 2, 5, 10]"
        :current-page="queryParams.pagenum"
        :page-size="queryParams.pagesize"
        :total="total"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog title="修改地址" width="50%"
      :visible.sync="setAddrDialogVisible"
      @close="closeSetAddrDialog"
    >
      <!-- 内容主体 -->
      <el-form
        :model="setAddrForm"
        ref="setAddrFormRef"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="regionData"
            :props="addrProps"
            v-model="setAddrForm.address1"
            @change="changeAddr"
            clearable
          />
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="setAddrForm.address2"/>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="setAddrDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setAddrDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" width="50%"
      :visible.sync="progressDialogVisible"
    >
      <!-- 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(activity, key) in progressData"
          :key="key"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
// https://github.com/Plortinus/element-china-area-data
import { regionData } from 'element-china-area-data'

export default {
  data () {
    const required = name => {
      return { required: true, message: `请输入${name}`, trigger: 'blur' }
    }
    return {
      // 订单列表
      orders: [],
      // 获取订单列表的请求对象
      queryParams: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      // 修改地址对话框
      setAddrDialogVisible: false,
      setAddrForm: {
        address1: [],
        address2: ''
      },
      rules: {
        address1: [ required('省市区/县') ],
        address2: [ required('详细地址') ]
      },
      // 地址级联选择器
      regionData,
      // 级联选择器配置对象
      addrProps: {
        expandTrigger: 'hover'
      },
      // 物流进度对话框
      progressDialogVisible: false,
      progressData: []
    }
  },
  created () {
    this.getOrders()
  },
  methods: {
    // 获取订单列表
    async getOrders () {
      const { data } = await this.$http.get('orders', { params: this.queryParams })
      if (data.meta.status !== 200) {
        return this.$message.error('获取订单列表失败，' + data.meta.msg)
      }
      this.orders = data.data.goods
      this.total = data.data.total
    },
    // 当前页码改变事件
    changePageSize (newSize) {
      this.queryParams.pagesize = newSize
      this.getOrders()
    },
    // 页面大小改变事件
    changePage (newPage) {
      this.queryParams.pagenum = newPage
      this.getOrders()
    },
    // 显示修改地址对话框
    showSetAddrDialog () {
      console.log(regionData)
      this.setAddrDialogVisible = true
    },
    // 关闭修改地址对话框
    closeSetAddrDialog () {
      this.$refs.setAddrFormRef.resetFields()
    },
    // 级联选择器改变事件
    changeAddr (val) {
      console.log(val)
    },
    // 显示物流进度对话框
    async showProgressDialog () {
      const { data } = await this.$http.get('kuaidi/804909574412544580') // 这个 api 返回死数据
      if (data.meta.status !== 200) {
        return this.$message.error('获取物流进度失败')
      }
      this.progressData = data.data
      this.progressDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
